package YAPclient;

import java.io.IOException;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;

import YAPenvironment.YAPconstant;
import YAPenvironment.YAPconstant.Command;

/**
 * This is an abstract class which provides easy access to important information on a client as well as to some
 * very basic methods. 
 * 
 * @author Xaro
 */

public abstract class DataModul{
	
	public static ParseThread parseThread;
	public static UDPReceiverThread udpReceiver;
	public static IsAliveThread isAliveThread;
	public static volatile boolean running=false;
	public static Client clPlayer;
	public static Socket sSocket;
	public static FormGameLobby fgl;
	public static FormLobbyOverview flo;
	public static GameInformation gInfo;
	public static final int sleepTime = 1000;
	
	/**
	 * new DatagramSocket
	 */
	public static DatagramSocket dSocket;
	public static InetAddress hostAddress;
	
	private static String sep = YAPconstant.C_SEPARATOR;
	public static boolean initialization(){
		try{
			parseThread = new ParseThread();
			isAliveThread = new IsAliveThread();
			gInfo = new GameInformation();
			clPlayer = new Client();
			
			start();
		}
		catch(Exception e){
			if(YAPconstant.C_DEBUG){ e.printStackTrace(); }
			return false;
		}
		return true;
	}
	
	private static void start(){
		if (parseThread != null){
			parseThread.start();
		}
		if (isAliveThread != null){
			isAliveThread.start();
		}
	}
	
	public static void disconnect(){
		System.out.println("***DISCONNECTING***");
		DataModul.parseThread.writeMessage(Command.disconnect+sep);
		running=false;
		try {
			sSocket.close();
			if(dSocket!=null)
				dSocket.close();
		} catch (SocketException e){
			//ignore
		} catch (IOException e) {
			if(YAPconstant.C_DEBUG){ e.printStackTrace(); }
		}
		sSocket = null;
	}
	
	public static void repaintFrames(){
//		if ((flo != null) && flo.isShowing()){
//			flo.repaint();
//		}
//		if ((fgl != null)) {//&& fgl.isShowing()){
//			fgl.repaint();
//		}
	}
}
